package com.xiaoshangkou.hellopra.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.pojo.Comment;
import com.xiaoshangkou.hellopra.pojo.Post;
import com.xiaoshangkou.hellopra.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/backstage/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    /**
     * 用途：进入帖子页面时分页查询所有帖子
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     */
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "6")int size){
        ModelAndView modelAndView=new ModelAndView();
        Page<Comment> commentPage = commentService.findPage(page, size);
        List<Comment> pageAll = commentService.findAll();
        commentPage.setRecords(pageAll);
        modelAndView.addObject("commentPage",commentPage);
        modelAndView.setViewName("/backstage/comment_all");
        return  modelAndView;
    }

    /**
     * 用途：根据帖子名称+评论作者搜索的功能
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     * title:帖子标题
     * 要改善
     */
    @RequestMapping("/search")
    public ModelAndView findPageByTitle(@RequestParam(defaultValue = "1")int page,
                                        @RequestParam(defaultValue = "6")int size,String titleAuthor){
        Page<Comment> commentPage = commentService.findPageByTitleAndAuthor(page, size, titleAuthor);
        List<Comment> byTitleAuthor = commentService.findByTitlAndAuthore(titleAuthor);
        commentPage.setRecords(byTitleAuthor);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("commentPage",commentPage);
        modelAndView.setViewName("/backstage/comment_search");
        return modelAndView;
    }
}
